#!/bin/bash

# 脚本名称：set_permissions.sh

# 显示帮助信息
usage() {
    echo "Usage: $0 [options] <directory>"
    echo "Options:"
    echo "  -e, --executable   Set all script files (sh, bash, py, pl, plx) to executable"
    echo "  -h, --help         Display this help message"
    echo "Example:"
    echo "  $0 -e /path/to/directory"
    exit 1
}

# 检查命令行参数
if [ $# -eq 0 ]; then
    echo "Error: No arguments provided."
    usage
fi

# 解析命令行参数
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -e|--executable)
            mode="executable"
            shift
            ;;
        -h|--help)
            usage
            ;;
        *)
            if [ -d "$1" ]; then
                directory="$1"
                shift
            else
                echo "Error: '$1' is not a valid directory."
                exit 1
            fi
            ;;
    esac
done

# 检查是否提供了目录
if [ -z "$directory" ]; then
    echo "Error: No directory specified."
    usage
fi

# 根据模式修改权限
case $mode in
    executable)
        echo "Setting all script files in '$directory' to executable..."
        find "$directory" -type f \( -name "*.sh" -o -name "*.bash" -o -name "*.py" -o -name "*.pl" -o -name "*.plx" \) -exec chmod +x {} \;
        ;;
    *)
        echo "Error: No valid mode specified."
        usage
        ;;
esac

echo "Permissions updated successfully." 